# -*- coding: utf-8 -*-
import sys

sys.path.append('../')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests, json
from common.helper import Helper


class Spider(object):

    def __init__(self):

        self.mysql = MysqlClient()
        self.name = "e无忧泰康全能保（少儿版）"

    # 生产产品数据
    def genProductInfo(self):
        plan = {
            "planA": {
                # 年龄段
                "year": [str(i) for i in range(0, 18)],
                # 性别
                "gender": ["M", "F"],
                # 有无社保
                "socialSec": ["Y", "N"],
                # 保障期限
                "expire": ["1年"],
                # 缴费类型
                "payType": ["年交"],
                # 产品计划
                "plan": ["普惠款"],
            },
            "planB": {
                # 年龄段
                "year": [str(i) for i in range(0, 18)],
                # 性别
                "gender": ["M", "F"],
                # 有无社保
                "socialSec": ["Y", "N"],
                # 保障期限
                "expire": ["1年"],
                # 缴费类型
                "payType": ["年交"],
                # 产品计划
                "plan": ["优享款"],
            },
            "planC": {
                # 年龄段
                "year": [str(i) for i in range(0, 18)],
                # 性别
                "gender": ["M", "F"],
                # 有无社保
                "socialSec": ["Y", "N"],
                # 保障期限
                "expire": ["1年"],
                # 缴费类型
                "payType": ["年交"],
                # 产品计划
                "plan": ["尊享款"],
            }
        }
        for k in plan:
            for v in product(plan[k]["year"], plan[k]["gender"], plan[k]["socialSec"], plan[k]["expire"],
                             plan[k]["payType"], plan[k]["plan"]):
                self.mysql.cursor.execute(
                    domain[self.name]["insert_sql"].format(v[0], v[1], v[2], v[3], v[4], v[5])
                )
                self.mysql._conn.commit()
                print(domain[self.name]["insert_sql"].format(v[0], v[1], v[2], v[3], v[4], v[5]))

    # 执行爬虫
    def requestJson(self, dt=None):

        if dt["plan"] == "普惠款":

            print("\n/*~~~普惠款~~~*/")

            data = {"calculateData": {"calstartDate": "2020-02-22 00:00:00", "insuredType": "famliy",
                                      "planId": "1310A00001", "calculateProductType": "1", "liabilityContents": [
                    {"liabilityId": "1007013", "amount": "100000", "liabilityName": "重疾保险金", "liabilityAmount": "10万"},
                    {"liabilityId": "1007002", "amount": "20000", "liabilityName": "轻症保险金", "liabilityAmount": "2万"},
                    {"liabilityId": "1013001", "amount": "500000", "liabilityName": "一般医疗保险金",
                     "liabilityAmount": "50万"},
                    {"liabilityId": "1013002", "amount": "500000", "liabilityName": "恶性肿瘤医疗保险金",
                     "liabilityAmount": "50万"},
                    {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                     "liabilityAmount": "1万"},
                    {"liabilityId": "1122031", "amount": "100000", "liabilityName": "意外身故/伤残",
                     "liabilityAmount": "10万"},
                    {"liabilityId": "1122032", "amount": "10000", "liabilityName": "意外医疗", "liabilityAmount": "1万"},
                    {"liabilityId": "1122001", "amount": "1000000", "liabilityName": "飞机意外身故/伤残",
                     "liabilityAmount": "100万"}]}, "insuredData": [
                {"index": 1, "insuredRelationship": "40", "insuredAge": dt["year"], "insuredGender": "1" if dt["gender"] == "M" else "2",
                 "insuredBirth": "2002-02-28", "minimum": "0", "maximum": "17", "planAgeMin": "0", "planAgeMax": "17",
                 "planLiabilityList": [
                     {"liabilityId": "1007013", "amount": "100000", "liabilityName": "重疾保险金", "liabilityAmount": "10万"},
                     {"liabilityId": "1007002", "amount": "20000", "liabilityName": "轻症保险金", "liabilityAmount": "2万"},
                     {"liabilityId": "1013001", "amount": "500000", "liabilityName": "一般医疗保险金",
                      "liabilityAmount": "50万"},
                     {"liabilityId": "1013002", "amount": "500000", "liabilityName": "恶性肿瘤医疗保险金",
                      "liabilityAmount": "50万"},
                     {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                      "liabilityAmount": "1万"},
                     {"liabilityId": "1122031", "amount": "100000", "liabilityName": "意外身故/伤残",
                      "liabilityAmount": "10万"},
                     {"liabilityId": "1122032", "amount": "10000", "liabilityName": "意外医疗", "liabilityAmount": "1万"},
                     {"liabilityId": "1122001", "amount": "1000000", "liabilityName": "飞机意外身故/伤残",
                      "liabilityAmount": "100万"}], "liabilityContents": [
                    {"liabilityId": "1007013", "amount": "100000", "liabilityName": "重疾保险金", "liabilityAmount": "10万"},
                    {"liabilityId": "1007002", "amount": "20000", "liabilityName": "轻症保险金", "liabilityAmount": "2万"},
                    {"liabilityId": "1013001", "amount": "500000", "liabilityName": "一般医疗保险金",
                     "liabilityAmount": "50万"},
                    {"liabilityId": "1013002", "amount": "500000", "liabilityName": "恶性肿瘤医疗保险金",
                     "liabilityAmount": "50万"},
                    {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                     "liabilityAmount": "1万"},
                    {"liabilityId": "1122031", "amount": "100000", "liabilityName": "意外身故/伤残",
                     "liabilityAmount": "10万"},
                    {"liabilityId": "1122032", "amount": "10000", "liabilityName": "意外医疗", "liabilityAmount": "1万"},
                    {"liabilityId": "1122001", "amount": "1000000", "liabilityName": "飞机意外身故/伤残",
                     "liabilityAmount": "100万"}], "socialSecurity": "1" if dt["socialSec"] == "Y" else "0"}],
                    "exchangeData": {"memberId": "", "memberToken": "", "applicationId": "1230655776607375360",
                                     "applicationToken": "1f855ec3eb541fb61aba37c62054b29b",
                                     "workqueueId": "1101753320687910912", "channelId": "3", "platformId": "pc",
                                     "referId": ""}, "userDefine": {"from": "details"}}
        elif dt["plan"] == "优享款":
            print("\n/*~~~优享款~~~*/")
            data = {"calculateData": {"calstartDate": "2020-02-22 00:00:00", "insuredType": "famliy",
                                      "planId": "1310A00001", "calculateProductType": "2", "liabilityContents": [
                    {"liabilityId": "1007013", "amount": "300000", "liabilityName": "重疾保险金", "liabilityAmount": "30万"},
                    {"liabilityId": "1007002", "amount": "60000", "liabilityName": "轻症保险金", "liabilityAmount": "6万"},
                    {"liabilityId": "1013001", "amount": "1000000", "liabilityName": "一般医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013002", "amount": "1000000", "liabilityName": "恶性肿瘤医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                     "liabilityAmount": "1万"},
                    {"liabilityId": "1122031", "amount": "200000", "liabilityName": "意外身故/伤残",
                     "liabilityAmount": "20万"},
                    {"liabilityId": "1122032", "amount": "20000", "liabilityName": "意外医疗", "liabilityAmount": "2万"},
                    {"liabilityId": "1122001", "amount": "5000000", "liabilityName": "飞机意外身故/伤残",
                     "liabilityAmount": "500万"}]}, "insuredData": [
                {"index": 1, "insuredRelationship": "40", "insuredAge": dt["year"], "insuredGender": "1" if dt["gender"] == "M" else "2",
                 "insuredBirth": "2002-02-28", "minimum": "0", "maximum": "17", "planAgeMin": "0", "planAgeMax": "17",
                 "planLiabilityList": [
                     {"liabilityId": "1007013", "amount": "300000", "liabilityName": "重疾保险金", "liabilityAmount": "30万"},
                     {"liabilityId": "1007002", "amount": "60000", "liabilityName": "轻症保险金", "liabilityAmount": "6万"},
                     {"liabilityId": "1013001", "amount": "1000000", "liabilityName": "一般医疗保险金",
                      "liabilityAmount": "100万"},
                     {"liabilityId": "1013002", "amount": "1000000", "liabilityName": "恶性肿瘤医疗保险金",
                      "liabilityAmount": "100万"},
                     {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                      "liabilityAmount": "1万"},
                     {"liabilityId": "1122031", "amount": "200000", "liabilityName": "意外身故/伤残",
                      "liabilityAmount": "20万"},
                     {"liabilityId": "1122032", "amount": "20000", "liabilityName": "意外医疗", "liabilityAmount": "2万"},
                     {"liabilityId": "1122001", "amount": "5000000", "liabilityName": "飞机意外身故/伤残",
                      "liabilityAmount": "500万"}], "liabilityContents": [
                    {"liabilityId": "1007013", "amount": "300000", "liabilityName": "重疾保险金", "liabilityAmount": "30万"},
                    {"liabilityId": "1007002", "amount": "60000", "liabilityName": "轻症保险金", "liabilityAmount": "6万"},
                    {"liabilityId": "1013001", "amount": "1000000", "liabilityName": "一般医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013002", "amount": "1000000", "liabilityName": "恶性肿瘤医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                     "liabilityAmount": "1万"},
                    {"liabilityId": "1122031", "amount": "200000", "liabilityName": "意外身故/伤残",
                     "liabilityAmount": "20万"},
                    {"liabilityId": "1122032", "amount": "20000", "liabilityName": "意外医疗", "liabilityAmount": "2万"},
                    {"liabilityId": "1122001", "amount": "5000000", "liabilityName": "飞机意外身故/伤残",
                     "liabilityAmount": "500万"}], "socialSecurity": "1" if dt["socialSec"]=="Y" else "0"}],
                    "exchangeData": {"memberId": "", "memberToken": "", "applicationId": "1230655776607375360",
                                     "applicationToken": "1f855ec3eb541fb61aba37c62054b29b",
                                     "workqueueId": "1101753320687910912", "channelId": "3", "platformId": "pc",
                                     "referId": ""}, "userDefine": {"from": "details"}}

        else:

            print("\n/*~~~尊享款~~~*/")
            data = {"calculateData": {"calstartDate": "2020-02-22 00:00:00", "insuredType": "famliy", "planId": "1310A00001",
                               "calculateProductType": "3", "liabilityContents": [
                    {"liabilityId": "1007013", "amount": "450000", "liabilityName": "重疾保险金", "liabilityAmount": "45万"},
                    {"liabilityId": "1007002", "amount": "100000", "liabilityName": "轻症保险金", "liabilityAmount": "10万"},
                    {"liabilityId": "1013001", "amount": "1000000", "liabilityName": "一般医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013002", "amount": "1000000", "liabilityName": "恶性肿瘤医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                     "liabilityAmount": "1万"},
                    {"liabilityId": "1122031", "amount": "500000", "liabilityName": "意外身故/伤残",
                     "liabilityAmount": "50万"},
                    {"liabilityId": "1122032", "amount": "30000", "liabilityName": "意外医疗", "liabilityAmount": "3万"},
                    {"liabilityId": "1122001", "amount": "5000000", "liabilityName": "飞机意外身故/伤残",
                     "liabilityAmount": "500万"}]}, "insuredData": [
                {"index": 1, "insuredRelationship": "40", "insuredAge":dt["year"], "insuredGender": "1" if dt["gender"] == "M" else "2",
                 "insuredBirth": "2006-10-10", "minimum": "0", "maximum": "17", "planAgeMin": "10", "planAgeMax": "17",
                 "planLiabilityList": [
                     {"liabilityId": "1007013", "amount": "450000", "liabilityName": "重疾保险金", "liabilityAmount": "45万"},
                     {"liabilityId": "1007002", "amount": "100000", "liabilityName": "轻症保险金", "liabilityAmount": "10万"},
                     {"liabilityId": "1013001", "amount": "1000000", "liabilityName": "一般医疗保险金",
                      "liabilityAmount": "100万"},
                     {"liabilityId": "1013002", "amount": "1000000", "liabilityName": "恶性肿瘤医疗保险金",
                      "liabilityAmount": "100万"},
                     {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                      "liabilityAmount": "1万"},
                     {"liabilityId": "1122031", "amount": "500000", "liabilityName": "意外身故/伤残",
                      "liabilityAmount": "50万"},
                     {"liabilityId": "1122032", "amount": "30000", "liabilityName": "意外医疗", "liabilityAmount": "3万"},
                     {"liabilityId": "1122001", "amount": "5000000", "liabilityName": "飞机意外身故/伤残",
                      "liabilityAmount": "500万"}], "liabilityContents": [
                    {"liabilityId": "1007013", "amount": "450000", "liabilityName": "重疾保险金", "liabilityAmount": "45万"},
                    {"liabilityId": "1007002", "amount": "100000", "liabilityName": "轻症保险金", "liabilityAmount": "10万"},
                    {"liabilityId": "1013001", "amount": "1000000", "liabilityName": "一般医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013002", "amount": "1000000", "liabilityName": "恶性肿瘤医疗保险金",
                     "liabilityAmount": "100万"},
                    {"liabilityId": "1013003", "amount": "10000", "liabilityName": "恶性肿瘤疾病给付保险金",
                     "liabilityAmount": "1万"},
                    {"liabilityId": "1122031", "amount": "500000", "liabilityName": "意外身故/伤残",
                     "liabilityAmount": "50万"},
                    {"liabilityId": "1122032", "amount": "30000", "liabilityName": "意外医疗", "liabilityAmount": "3万"},
                    {"liabilityId": "1122001", "amount": "5000000", "liabilityName": "飞机意外身故/伤残",
                     "liabilityAmount": "500万"}], "socialSecurity": "0" if dt["socialSec"] == "N" else "1"}],
             "exchangeData": {"memberId": "", "memberToken": "", "applicationId": "1230655776607375360",
                              "applicationToken": "1f855ec3eb541fb61aba37c62054b29b",
                              "workqueueId": "1101753320687910912", "channelId": "3", "platformId": "pc",
                              "referId": ""}, "userDefine": {"from": "details"}}

        payload = json.dumps(data)
        r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
        if r.status_code == 200 and '保费试算成功' in r.text:
            price = Helper.decimalParse(r.json()["data"]["sumPremium"])
            data = {
                "name":self.name,
                "id":dt["id"],
                "price":price
            }
            self.mysql.updatePrice( data )
        else:
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 3
            }
            self.mysql.updateTask( data )
            print("\n/*更新产品价格失败:%d*/"%dt["id"])

    def main(self):

        while True:

            # 随机领取一条数据
            dt = self.mysql.randTask(self.name)

            if not dt:
                print("产品价格抓取完成~~~")

                return

            # 任务执行状态
            data = {
                "name": self.name,
                "id": dt["id"],
                "state": 1
            }
            self.mysql.updateTask(data)

            self.requestJson(dt)
